package com.example.demoweb.controller;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import sun.misc.BASE64Encoder;

@SuppressWarnings("restriction")
public class FileTest {
	
	public static void main(String[] args) {
        String filePath = "d://timg.jpg";//待处理的图片  
        InputStream in = null;  
        try {
        	in = new FileInputStream(filePath);
            // in.available()返回文件的字节长度
        	int length = in.available();
            byte[] bytes = new byte[length];
            // 将文件中的内容读入到数组中再转为字符串
            in.read(bytes);
            //将字节流数组转换为字符串
            BASE64Encoder base64Encoder = new BASE64Encoder();
            String strBase64 = base64Encoder.encode(bytes);   
            System.out.println(strBase64);
            in.close();
        } catch (FileNotFoundException fe) {
            fe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
	}
}
